package hk.ust.comp4521.exust;

import hk.ust.comp4521.exust.data.Database;
import android.app.Activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Record extends BaseFragment
{
	private EditText recordText;
	private TextView showText;
	private Button confirmButton;
	private Button deleteButton;
	int position;
	CalendarFragment calendarFragment;

	public void setParam(int position, CalendarFragment calendarFragment)
	{
		this.position = position;
		this.calendarFragment = calendarFragment;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.record, null);

		recordText = (EditText) view.findViewById(R.id.editText1);
		showText = (TextView) view.findViewById(R.id.editText2);

		confirmButton = (Button) view.findViewById(R.id.submitbutton);
		deleteButton = (Button) view.findViewById(R.id.deletebutton);

		confirmButton.setOnClickListener(confirmButtonListener);
		deleteButton.setOnClickListener(deleteButtonListener);

		// String result = recordText.getText().toString();
		//
		showText.setText(calendarFragment.eventContents[position]);

		return view;
	}

	@Override
	public String getTitle()
	{
		return "Calendar";
	}

	private OnClickListener confirmButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			String hourStrFrom = getHour(position);
			String hourStrTo = getHour(position + 8);
			String dayStr = getDay(position);

			String result = recordText.getText().toString();
			String FromTo = "From" + ":    " + hourStrFrom + '\n' + "To   "
					+ ":    " + hourStrTo;

			String output = dayStr + "\n\n" + FromTo + "\n\n" + "content:"
					+ "\n\n" + result;

			if (!result.equals(""))
			{
				calendarFragment.events8m25[position] = "F";
				calendarFragment.eventContents[position] = output;
				calendarFragment.syncEvents7m24WithEvents8m25();
				
				Database.getUser().setCalendar(calendarFragment.getEvents());
				Database.commitUser();
				
			}

			((MainActivity) getActivity()).popFragment();

		}
	};

	private OnClickListener deleteButtonListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{

			recordText.setText("");

			calendarFragment.events8m25[position] = "";
			calendarFragment.eventContents[position] = "";
			calendarFragment.syncEvents7m24WithEvents8m25();

			((MainActivity) getActivity()).popFragment();

		}
	};

	private String getDay(int position)
	{
		int dayInt = position % 8;

		String dayStr = null;

		if (dayInt == 1)
			dayStr = "Monday";
		else if (dayInt == 2)
			dayStr = "Tuesday";
		else if (dayInt == 3)
			dayStr = "Wednesday";
		else if (dayInt == 4)
			dayStr = "Thursday";
		else if (dayInt == 5)
			dayStr = "Friday";
		else if (dayInt == 6)
			dayStr = "Saturday";
		else if (dayInt == 7)
			dayStr = "Sunday";

		return dayStr;
	}

	private String getHour(int position)
	{
		int hourInt = position / 8;
		hourInt = hourInt - 1;

		String hourStr;

		if (hourInt < 10)
			hourStr = '0' + String.valueOf(hourInt) + ":00";
		else
			hourStr = String.valueOf(hourInt) + ":00";
		return hourStr;
	}

}